﻿Imports System.Drawing.Drawing2D
Imports System.ComponentModel


Public Class PTIC_DataGridView

#Region "Enumérations"
    Public Enum nomThemeDataGridView
        OMANE
        Standard
    End Enum
#End Region

#Region "Variables de classe"
#Region "Affichage"
    Private oldRowIndex As Integer = 0
#End Region
#Region "Thème"
    Private _theme As ThemeComposant
    Private _nomTheme As nomThemeDataGridView
    Private _isSelectionable As Boolean
#End Region
#End Region

#Region "Constructeur"

    Public Sub New()

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        'Activation du double buffering
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.ResizeRedraw, True)


        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        'Me.EnableHeadersVisualStyles = True
        'Dim headerStyle As New DataGridViewCellStyle()
        'headerStyle.BackColor = Color.BlueViolet
        'headerStyle.ForeColor = Color.Lavender
        'headerStyle.Font = New Font("Tahoma", 9, FontStyle.Bold)
        'headerStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        'A paramétrer lorsque le style est "selectionable"
        If Me._isSelectionable Then
            Me.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Transparent
        Else
            Me.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.White
        End If

        Me.RowTemplate.DefaultCellStyle.SelectionForeColor = Color.Black
        Me.CellBorderStyle = DataGridViewCellBorderStyle.None

        oldRowIndex = 0
        nomStyle = nomThemeDataGridView.OMANE
        Me.ColumnHeadersDefaultCellStyle.Font = New Font(_theme.styleEnCours.font.FontFamily, _theme.styleEnCours.font.Size, FontStyle.Bold)
        'Me._isSelectionable = True

    End Sub

#End Region

#Region "Propriétés"

    <Browsable(True), Category("Thème")> _
    Public Property nomStyle() As nomThemeDataGridView

        Get
            Return _nomTheme
        End Get

        Set(ByVal value As nomThemeDataGridView)

            _nomTheme = value

            If value = nomThemeDataGridView.OMANE Then

                Dim styleDgv_OMANE_STD As _
                New styleEtatComposant(My.Settings.OMANE_Title2BackColor, _
                                       My.Settings.OMANE_FontBase, _
                                       My.Settings.OMANE_STDForeColor, _
                                       My.Settings.OMANE_ColorBorder, _
                                       Nothing)
                'Dim styleDgv_OMANE_STD As _
                'New styleEtatComposant(My.Settings.OMANEBtnSTD_ColorDegrade, _
                '                       My.Settings.OMANE_FontBase, _
                '                       Color.White, _
                '                       My.Settings.OMANE_ColorBorder, _
                '                       Nothing)

                Dim styleDgvOMANE_MOUSEOVER As _
                        New styleEtatComposant(My.Settings.OMANERdbMOUSEOVER_ColorDegrade1, _
                                               My.Settings.OMANERdbMOUSEOVER_ColorDegrade2, _
                                               My.Settings.OMANERdbMOUSEOVER_ColorDegrade3, _
                                               My.Settings.OMANERdbMOUSEOVER_ColorDegrade4, _
                                               My.Settings.OMANE_FontBase, _
                                               My.Settings.OMANEBtnSTD_ColorFore, _
                                               My.Settings.OMANE_ColorBorder, _
                                               Nothing)
                Dim styleDgvOMANE_DIS As _
                        New styleEtatComposant(Nothing, _
                                               Nothing, _
                                               Nothing, _
                                               Nothing, _
                                               Nothing)

                _theme = New ThemeComposant(styleDgv_OMANE_STD, _
                                            styleDgvOMANE_MOUSEOVER, _
                                            styleDgvOMANE_DIS, etatComposant.Enabled)

            End If

            Invalidate()

        End Set

    End Property

    <Browsable(True), Category("Thème")> _
    Public Property isSelectionable() As Boolean
        Get
            Return _isSelectionable
        End Get
        Set(ByVal value As Boolean)
            _isSelectionable = value
        End Set
    End Property

#End Region

    'Evènement déclanché avant le dessin d'une ligne
    Private Sub PTIC_DataGridView_RowPrePaint(ByVal sender As Object, _
                                              ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles Me.RowPrePaint

        If _isSelectionable Then

            Dim g As Graphics = e.Graphics

            'Désactive le rafraichissement automatique du rectangle de focus
            e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus

            'Test si la ligne est sélectionnée ou non - dans l'affirmative, les cellules doivent être
            'repeintes avec la couleur de sélection personnalisé
            If (e.State And DataGridViewElementStates.Selected) = _
                DataGridViewElementStates.Selected Then

                'Calcule les dimmensions de la ligne (en prenant en compte le scrolling horizontale
                '(dépassement de la dgv par la droite)
                Dim rowBounds As New Rectangle( _
                    Me.RowHeadersWidth, e.RowBounds.Top, _
                    Me.Columns.GetColumnsWidth( _
                    DataGridViewElementStates.Visible) - _
                    Me.HorizontalScrollingOffset, _
                    e.RowBounds.Height)

                Try
                    'Remplissage de la ligne avec un dégradé
                    Styles.PaintDoubleDegrade(g, rowBounds, _theme.styleMouseOver.backColor, _
                                              _theme.styleMouseOver.backColor2, _
                                              _theme.styleMouseOver.backColor3, _
                                              _theme.styleMouseOver.backColor4, 0.5)
                Finally
                End Try
            End If
        End If
    End Sub

    Private Sub PTIC_DataGridView_CellPainting(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Me.CellPainting


        Dim g As Graphics = e.Graphics

        If e.ColumnIndex = -1 Or e.RowIndex = -1 Then

            Dim backColorBrush As New LinearGradientBrush(e.CellBounds, Color.White, _
                                      Color.FromArgb(191, 219, 255), LinearGradientMode.Vertical)
            Dim gridBrush As New SolidBrush(_theme.styleEnCours.borderColor)
            Dim gridLinePen As New Pen(gridBrush)

            Try

                ' Erase the cell.
                Styles.PaintBackAeroGlassOMANE(g, e.CellBounds, _theme.styleEnCours.backColor)
                ''g.FillRectangle(backColorBrush, e.CellBounds)

                ' Draw the grid lines (only the right and bottom lines;
                ' DataGridView takes care of the others).
                g.DrawLine(gridLinePen, e.CellBounds.Left, _
                    e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
                    e.CellBounds.Bottom - 1)

                g.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
                    e.CellBounds.Top, e.CellBounds.Right - 1, _
                    e.CellBounds.Bottom - 1)

                'Dessine le contenu de la cellule en alignant le texte verticalement
                'If e.ColumnIndex = -1 Then e.Value = e.RowIndex
                If (e.Value IsNot Nothing Or (e.ColumnIndex = -1 And e.RowIndex >= 0)) Then

                    Dim fontTitre As Font
                    With _theme.styleEnCours.font
                        fontTitre = New Font(.FontFamily, .Size, FontStyle.Bold)
                    End With

                    Dim valTxt As String = CStr(IIf((e.ColumnIndex = -1 And e.RowIndex >= 0), e.RowIndex + 1, e.Value))
                    Dim SizeTxt As Size = TextRenderer.MeasureText(valTxt, fontTitre)
                    'Dim brushTxt As New System.Drawing.Brushes

                    ''g.DrawString(valTxt, fontTitre, _
                    ''New SolidBrush(_theme.styleEnCours.foreColor), _
                    ''e.CellBounds.X + 2, e.CellBounds.Y + (e.CellBounds.Height - SizeTxt.Height) / 2, _
                    ''StringFormat.GenericDefault)
                    Dim rectText As Rectangle
                    With ColumnHeadersDefaultCellStyle.Padding

                        rectText = New Rectangle(e.CellBounds.X + .Left, _
                                                 e.CellBounds.Y + .Top, _
                                                 e.CellBounds.Width - .Left - .Right, _
                                                 e.CellBounds.Height - .Top - .Bottom)
                    End With

                    Dim stringFormat As New StringFormat()
                    stringFormat.Alignment = StringAlignment.Near
                    stringFormat.LineAlignment = StringAlignment.Center

                    g.DrawString(valTxt, fontTitre, _
                        New SolidBrush(_theme.styleEnCours.foreColor), _
                        rectText, stringFormat)

                End If

                e.Handled = True

            Finally
                gridLinePen.Dispose()
                gridBrush.Dispose()
                backColorBrush.Dispose()
            End Try

        End If

    End Sub

    Private Sub PTIC_DataGridView_RowPostPaint(ByVal sender As Object, _
                                               ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles Me.RowPostPaint

        If _isSelectionable Then

            Dim g As Graphics = e.Graphics

            'Calcule les dimensions de la ligne
            Dim rowBounds As New Rectangle(Me.RowHeadersWidth, _
                e.RowBounds.Top - 1, Me.Columns.GetColumnsWidth( _
                DataGridViewElementStates.Visible) - _
                Me.HorizontalScrollingOffset + 1, e.RowBounds.Height)

            Dim forebrush As SolidBrush = Nothing
            Try
                ' Determine la couleur de la police
                If (e.State And DataGridViewElementStates.Selected) = _
                    DataGridViewElementStates.Selected Then
                    forebrush = New SolidBrush(e.InheritedRowStyle.SelectionForeColor)
                Else
                    forebrush = New SolidBrush(e.InheritedRowStyle.ForeColor)
                End If

                ' Get the content that spans multiple columns.
                Dim recipe As Object = _
                    Me.Rows.SharedRow(e.RowIndex).Cells(2).Value

                If (recipe IsNot Nothing) Then
                    Dim text As String = recipe.ToString()

                    ' Calculate the bounds for the content that spans multiple 
                    ' columns, adjusting for the horizontal scrolling position 
                    ' and the current row height, and displaying only whole
                    ' lines of text.
                    Dim textArea As Rectangle = rowBounds
                    textArea.X -= Me.HorizontalScrollingOffset
                    textArea.Width += Me.HorizontalScrollingOffset
                    textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
                    textArea.Height -= rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
                    textArea.Height = (textArea.Height \ e.InheritedRowStyle.Font.Height) * _
                        e.InheritedRowStyle.Font.Height

                    ' Calculate the portion of the text area that needs painting.
                    Dim clip As RectangleF = textArea
                    clip.Width -= Me.RowHeadersWidth + 1 - clip.X
                    clip.X = Me.RowHeadersWidth + 1
                    Dim oldClip As RectangleF = e.Graphics.ClipBounds
                    g.SetClip(clip)

                    ' Draw the content that spans multiple columns.
                    g.DrawString(text, e.InheritedRowStyle.Font, forebrush, _
                        textArea)

                    g.SetClip(oldClip)
                End If
            Catch
            Finally
                forebrush.Dispose()
            End Try

            If Me.CurrentCellAddress.Y = e.RowIndex Then

                Dim rowBoundsFocus As New Rectangle(Me.RowHeadersWidth, _
                e.RowBounds.Top, Me.Columns.GetColumnsWidth( _
                DataGridViewElementStates.Visible) - _
                Me.HorizontalScrollingOffset, e.RowBounds.Height)
                e.DrawFocus(rowBoundsFocus, True)

            End If

            'C'est à cet endroit que l'on peut ajouter les bordures personnalisées (pointillées)
        End If
    End Sub

    ' Forces the row to repaint itself when the user changes the 
    ' current cell. This is necessary to refresh the focus rectangle.
    Private Sub PTIC_DataGridView_CurrentCellChanged(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Me.CurrentCellChanged

        If _isSelectionable Then

            If oldRowIndex <> -1 Then
                InvalidateRow(oldRowIndex)
            End If

            oldRowIndex = Me.CurrentCellAddress.Y

        End If

    End Sub


End Class

'Public Class toto
'Non utilisé
'Public ReadOnly Property test()
''Public Overrides ReadOnly Property AdjustedTopLeftHeaderBorderStyle() _
''   As DataGridViewAdvancedBorderStyle
''    Get
''Dim newStyle As New DataGridViewAdvancedBorderStyle()
''        With newStyle
''            .Top = DataGridViewAdvancedCellBorderStyle.None
''            .Left = DataGridViewAdvancedCellBorderStyle.None
''            .Bottom = DataGridViewAdvancedCellBorderStyle.OutsetDouble
''            .Right = DataGridViewAdvancedCellBorderStyle.OutsetDouble
''        End With
''        Return newStyle
''    End Get
''End Property
'End Class
'Non utilisé
''Public Overrides Function AdjustColumnHeaderBorderStyle( _
''    ByVal dataGridViewAdvancedBorderStyleInput As DataGridViewAdvancedBorderStyle, _
''    ByVal dataGridViewAdvancedBorderStylePlaceHolder As DataGridViewAdvancedBorderStyle, _
''    ByVal firstDisplayedColumn As Boolean, ByVal lastVisibleColumn As Boolean) _
''    As DataGridViewAdvancedBorderStyle

' Customize the left border of the first column header and the
' bottom border of all the column headers. Use the input style for 
' all other borders.
''    If firstDisplayedColumn Then
''        dataGridViewAdvancedBorderStylePlaceHolder.Left = _
''            DataGridViewAdvancedCellBorderStyle.OutsetDouble
''    Else
''        dataGridViewAdvancedBorderStylePlaceHolder.Left = _
''            DataGridViewAdvancedCellBorderStyle.None
''    End If

''    With dataGridViewAdvancedBorderStylePlaceHolder
''        .Bottom = DataGridViewAdvancedCellBorderStyle.Single
''        .Right = dataGridViewAdvancedBorderStyleInput.Right
''        .Top = dataGridViewAdvancedBorderStyleInput.Top
''    End With

''    Return dataGridViewAdvancedBorderStylePlaceHolder
''End Function


'Non utilisé
''Public Class DataGridViewCustomColumn
''Inherits DataGridViewColumn

''Public Sub New()
''    Me.CellTemplate = New DataGridViewCustomCell()
''End Sub

''End Class

'Non utilisé
''Public Class DataGridViewCustomCell
''Inherits DataGridViewTextBoxCell

''Public Overrides Function AdjustCellBorderStyle( _
''    ByVal dataGridViewAdvancedBorderStyleInput As DataGridViewAdvancedBorderStyle, _
''    ByVal dataGridViewAdvancedBorderStylePlaceHolder As DataGridViewAdvancedBorderStyle, _
''    ByVal singleVerticalBorderAdded As Boolean, _
''    ByVal singleHorizontalBorderAdded As Boolean, _
''    ByVal firstVisibleColumn As Boolean, _
''    ByVal firstVisibleRow As Boolean) As DataGridViewAdvancedBorderStyle

' Customize the top border of cells in the first row and the 
' right border of cells in the first column. Use the input style 
' for all other borders.
''    If firstVisibleColumn Then
''        dataGridViewAdvancedBorderStylePlaceHolder.Left = _
''            DataGridViewAdvancedCellBorderStyle.OutsetDouble
''    Else
''        dataGridViewAdvancedBorderStylePlaceHolder.Left = _
''            DataGridViewAdvancedCellBorderStyle.None
''    End If

''    If firstVisibleRow Then
''        dataGridViewAdvancedBorderStylePlaceHolder.Top = _
''            DataGridViewAdvancedCellBorderStyle.InsetDouble
''    Else
''        dataGridViewAdvancedBorderStylePlaceHolder.Top = _
''            DataGridViewAdvancedCellBorderStyle.None
''    End If

''    With dataGridViewAdvancedBorderStylePlaceHolder
''        .Right = dataGridViewAdvancedBorderStyleInput.Right
''        .Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
''    End With

''    Return dataGridViewAdvancedBorderStylePlaceHolder
''End Function

''End Class

'Non utilisé
''Public Class DataGridViewCustomRow
''Inherits DataGridViewRow

''Public Overrides Function AdjustRowHeaderBorderStyle( _
''    ByVal dataGridViewAdvancedBorderStyleInput As DataGridViewAdvancedBorderStyle, _
''    ByVal dataGridViewAdvancedBorderStylePlaceHolder As DataGridViewAdvancedBorderStyle, _
''    ByVal singleVerticalBorderAdded As Boolean, _
''    ByVal singleHorizontalBorderAdded As Boolean, _
''    ByVal isFirstDisplayedRow As Boolean, _
''    ByVal isLastDisplayedRow As Boolean) As DataGridViewAdvancedBorderStyle

' Customize the top border of the first row header and the
' right border of all the row headers. Use the input style for 
' all other borders.
''    If isFirstDisplayedRow Then
''        dataGridViewAdvancedBorderStylePlaceHolder.Top = _
''            DataGridViewAdvancedCellBorderStyle.InsetDouble
''    Else
''        dataGridViewAdvancedBorderStylePlaceHolder.Top = _
''            DataGridViewAdvancedCellBorderStyle.None
''    End If

''    With dataGridViewAdvancedBorderStylePlaceHolder
''        .Right = DataGridViewAdvancedCellBorderStyle.OutsetDouble
''        .Left = dataGridViewAdvancedBorderStyleInput.Left
''        .Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
''    End With

''    Return dataGridViewAdvancedBorderStylePlaceHolder
''End Function

''End Class

